package com.ruoyi.generator.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.generator.entity.Equipment;
import com.ruoyi.generator.entity.MaintenancePlan;
import com.ruoyi.generator.entity.Region;
import com.ruoyi.generator.entity.Task;
import com.ruoyi.generator.service.ManagementService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FileDownload {
    @Autowired
    private ManagementService managementService;

    @PostMapping("/api/common/download")
    public void downloadData(HttpServletResponse response) {
        // 创建查询条件
        Region region = new Region();
        region.setIsDelete("0");
        Equipment equipment = new Equipment();
        equipment.setIsDelete("0");
        Task task = new Task();
        task.setIsDelete("0");
        MaintenancePlan maintenancePlan = new MaintenancePlan();
        maintenancePlan.setIsDelete("0");

        // 查询每个表的数据
        List<Region> regions = managementService.selectRegionList(region);
        List<Equipment> equipments = managementService.selectEquipmentList(equipment);
        List<Task> tasks = managementService.selectTaskList(task);
        List<MaintenancePlan> maintenancePlans = managementService.selectMaintenancePlanList(maintenancePlan);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"data.xlsx\"");

        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建数据工作表
            createRegionSheet(workbook, regions);      // 区域数据
            createEquipmentSheet(workbook, equipments); // 设备数据
            createTaskSheet(workbook, tasks);         // 任务数据
            createMaintenancePlanSheet(workbook, maintenancePlans);         // 任务数据

            // 将工作簿输出到响应流
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建区域数据的工作表
     */
    private void createRegionSheet(Workbook workbook, List<Region> regions) {
        Sheet sheet = workbook.createSheet("区域数据");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"区域ID", "区域名称"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据行
        for (int i = 0; i < regions.size(); i++) {
            Row dataRow = sheet.createRow(i + 1);
            Region region = regions.get(i);

            dataRow.createCell(0).setCellValue(region.getAreaId());
            dataRow.createCell(1).setCellValue(region.getAreaName());
//            dataRow.createCell(2).setCellValue(region.getAreaDescription());
//            dataRow.createCell(3).setCellValue(region.getRegion());
//            dataRow.createCell(4).setCellValue(region.getLongitude());
//            dataRow.createCell(5).setCellValue(region.getLatitude());
//            dataRow.createCell(6).setCellValue(region.getIsDelete());
        }
    }

    /**
     * 创建设备数据的工作表
     */
    private void createEquipmentSheet(Workbook workbook, List<Equipment> equipments) {
        Sheet sheet = workbook.createSheet("设备数据");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"区域ID","设备名称", "设备类型", "设备ID"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据行
        for (int i = 0; i < equipments.size(); i++) {
            Row dataRow = sheet.createRow(i + 1);
            Equipment equipment = equipments.get(i);

            dataRow.createCell(0).setCellValue(equipment.getAreaId());
            dataRow.createCell(1).setCellValue(equipment.getEquipmentName());
            dataRow.createCell(2).setCellValue(equipment.getEquipmentType());
            dataRow.createCell(3).setCellValue(equipment.getEquipmentId());
        }
    }

    /**
     * 创建任务数据的工作表
     */
    private void createTaskSheet(Workbook workbook, List<Task> tasks) {
        Sheet sheet = workbook.createSheet("任务数据");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"任务ID", "设备ID", "检查点", "检查项", "设定值"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据行
        for (int i = 0; i < tasks.size(); i++) {
            Row dataRow = sheet.createRow(i + 1);
            Task task = tasks.get(i);

            dataRow.createCell(0).setCellValue(task.getTaskId());
            dataRow.createCell(1).setCellValue(task.getEquipmentId());
            dataRow.createCell(2).setCellValue(task.getCheckPoint());
            dataRow.createCell(3).setCellValue(task.getCheckItem());
            dataRow.createCell(4).setCellValue(task.getSetValue());

        }
    }
    /**
     * 创建保养数据的工作表
     */
    private void createMaintenancePlanSheet(Workbook workbook, List<MaintenancePlan> maintenancePlanList) {
        Sheet sheet = workbook.createSheet("保养数据");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"设备ID", "保养ID", "检查点", "检查项", "设定值"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据行
        for (int i = 0; i < maintenancePlanList.size(); i++) {
            Row dataRow = sheet.createRow(i + 1);
            MaintenancePlan maintenancePlan = maintenancePlanList.get(i);

            dataRow.createCell(0).setCellValue(maintenancePlan.getEquipmentId());
            dataRow.createCell(1).setCellValue(maintenancePlan.getMaintenanceId());
            dataRow.createCell(2).setCellValue(maintenancePlan.getMaintenancePoint());
            dataRow.createCell(3).setCellValue(maintenancePlan.getMaintenanceItem());
            dataRow.createCell(4).setCellValue(maintenancePlan.getSetValue());

        }
    }
}
